package cn.ctyun.ipush.service.impl;

import cn.ctyun.ipush.dao.ExcelDao;
import cn.ctyun.ipush.dao.PictureDao;
import cn.ctyun.ipush.dao.TaskDao;
import cn.ctyun.ipush.model.DataSourceModel;
import cn.ctyun.ipush.model.ExcelModel;
import cn.ctyun.ipush.model.PictureModel;
import cn.ctyun.ipush.service.ExcelService;
import cn.ctyun.ipush.service.PictureService;
import cn.ctyun.ipush.utils.DateUtils;
import cn.ctyun.ipush.vo.ExcelVo;
import cn.ctyun.ipush.vo.PictureVo;
import com.dexcoder.commons.bean.BeanConverter;
import com.dexcoder.commons.utils.UUIDUtils;
import com.dexcoder.dal.build.Criteria;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 16-7-11
 * Time: 上午9:45
 * To change this template use File | Settings | File Templates.
 */
@Service
public class PictureServiceImpl implements PictureService {


    @Autowired
    private PictureDao pictureDao;

    @Autowired
    private TaskDao taskDao;


    @Override
    public void save(PictureVo pictureVo) {
        pictureVo.setPictureModelId(UUIDUtils.getUUID32());
        pictureVo.setCreateDate(DateUtils.dateToString(new Date()));
        PictureModel pictureModel = pictureVo.getTargetObject(PictureModel.class);
        //保存到数据库中
        save(pictureModel);
    }

    @Override
    public String save(PictureModel pictureModel) {
        pictureModel.setCreateDate(DateUtils.dateToString(new Date()));
        pictureModel.setIsDeleted("0");
        pictureModel.setPictureModelId(UUIDUtils.getUUID32());
        pictureDao.save(pictureModel);
        return pictureModel.getPictureModelId();
    }

    @Override
    public int delete(String pictureId) {
        //判断是否存在引用，是否在task中.如果有引用肯定存在Job，此时不删除；
        if (taskDao.isExistPictureInTask(pictureId)) {
            return 0;
        }
        //不存在，删除缓存，数据库记录删除；无引用肯定也不存在对应的Job
        // DBUtils.deleteConnectionCache(dataSourceDao.querySingleResult(dataSourceId));
        //数据库记录软删除
        return pictureDao.delete(pictureId);
    }

    @Override
    public int update(PictureVo pictureVo) {
        if (StringUtils.isEmpty(pictureVo.getPictureModelId())) {
            throw new RuntimeException("Picture 主键ID 不存在");
        }
        //todo 额外处理
        PictureModel pictureModel = pictureVo.getTargetObject(PictureModel.class);
        PictureModel pictureModelDb = querySingleResult(pictureModel.getPictureModelId());

        if (pictureModel.getPictureName() != null)
            pictureModelDb.setPictureName(pictureModel.getPictureName());
        if (pictureModel.getPicturePix() != null)
            pictureModelDb.setPicturePix(pictureModel.getPicturePix());

        return pictureDao.update(pictureModelDb);
    }

    @Override
    public int update(PictureModel pictureModel) {
        if (StringUtils.isEmpty(pictureModel.getPictureModelId())) {
            throw new RuntimeException("Picture 主键ID 不存在");
        }
        //todo 额外处理
        PictureModel pictureModelDb = querySingleResult(pictureModel.getPictureModelId());

        if (pictureModel.getPictureName() != null)
            pictureModelDb.setPictureName(pictureModel.getPictureName());
        if (pictureModel.getPicturePix() != null)
            pictureModelDb.setPicturePix(pictureModel.getPicturePix());

        if(pictureModel.getPicSheetNames()!=null){
            pictureModelDb.setPicSheetNames(pictureModel.getPicSheetNames());
        }


        return pictureDao.update(pictureModelDb);
    }


    @Override
    public PictureVo querySingleResultVo(String pictureId) {
        PictureModel pictureModel = querySingleResult(pictureId);
        return pictureModel.getTargetObject(PictureVo.class);
    }

    @Override
    public PictureModel querySingleResult(String pictureId) {
        return pictureDao.querySingleResult(pictureId);
    }

    @Override
    public List<PictureModel> queryList(PictureModel pictureModel) {
        Criteria criteria = Criteria.select(PictureModel.class).where("pictureName", "  like ", new Object[]{"%" + pictureModel.getPictureName() + "%"}).where("and isDeleted", new Object[]{"0"});
        return pictureDao.queryList(criteria);
    }

    @Override
    public List<PictureVo> queryListVo(PictureVo pictureVo) {
        List<PictureModel> pictureModelList = queryList(pictureVo.getTargetObject(PictureModel.class));
        List<PictureVo> pictureVoList = BeanConverter.convert(PictureVo.class, pictureModelList);
        return pictureVoList;
    }

    @Override
    public boolean isExist(PictureVo pictureVo) {
        PictureModel pictureModel = pictureVo.getTargetObject(PictureModel.class);
        if (null == pictureDao.querySingleResultByName(pictureModel.getPictureName())) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public List<PictureVo> queryAll() {
        List<PictureModel> pictureModelList = pictureDao.queryAll(new PictureModel());
        List<PictureVo> pictureVoList = BeanConverter.convert(PictureVo.class, pictureModelList);
        return pictureVoList;
    }

    @Override
    public List<PictureVo> queryListVoByPage(PictureVo pictureVo) {
        //分页查询
        int currentPage = pictureVo.getCurPage();
        int itemPages = pictureVo.getItemsPerPage();
        List<PictureModel> pictureModelList = pictureDao.queryListByPage(currentPage, itemPages, pictureVo);
        List<PictureVo> pictureVoList = BeanConverter.convert(PictureVo.class, pictureModelList);
        return pictureVoList;
    }
}
